<?php

/**
 * bulletin_board actions.
 *
 * @package    ecollegeplus
 * @subpackage bulletin_board
 * @author     Batanayi Matuku
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class bulletin_boardActions extends autoBulletin_boardActions
{

    public function executeTab(sfWebRequest $request)
    {
        $this->bulletin_boards = BulletinBoardPostTable::getInstance()->retrieveBulletinBoardsExceptTrashed($this->getUser()->getId());
    }

    public function executeCreate(sfWebRequest $request)
    {
        $this->form = $this->configuration->getForm();
        $this->bulletin_board_post = $this->form->getObject();

        $this->processForm($request, $this->form);
    }

    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        myToolkit::debug($request->getParameter($form->getName()));
        
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid())
        {
            $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

            try
            {
                $this->bulletin_board_post = $form->save();
                die($this->bulletin_board_post->getPost());
            }
            catch (Doctrine_Validator_Exception $e)
            {
                die($e);
            }

            $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $this->bulletin_board_post)));
        }
        else
        {
            $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
        }
    }

    public function executeUndoRemove(sfWebRequest $request)
    {
        $bulletin_board_post_id = $request['id'];
        if ($bulletin_board_post_id && $this->getUser()->getId())
        {
            $bulletin_board_trash = BulletinBoardTrashTable::getInstance()->findOneByBulletinBoardAndUserId($bulletin_board_post_id, $this->getUser()->getId());
            if ($bulletin_board_trash)
            {
                try
                {
                    $bulletin_board_trash->delete();
                }
                catch (Exception $e)
                {
                    die("failure");
                }
            }
        }
        die("success");
    }

    public function executeRemove(sfWebRequest $request)
    {
        $bulletin_board_post_id = $request['id'];
        if ($bulletin_board_post_id && $this->getUser()->getId())
        {
            $bulletin_board_trash = BulletinBoardTrashTable::getInstance()->findOneByBulletinBoardAndUserId($bulletin_board_post_id, $this->getUser()->getId());
            if (!is_object($bulletin_board_trash))
            {
                $bulletin_board_trash = new BulletinBoardTrash();
                $bulletin_board_trash->setUserId($this->getUser()->getId());
                $bulletin_board_trash->setBulletinBoardPostId($bulletin_board_post_id);
                $bulletin_board_trash->save();
            }
        }
        die("success");
    }

}
